/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Resultados.java
 *
 * Created on 08-oct-2011, 22:36:07
 */
package Principal.LoteEconomico;

import java.awt.FlowLayout;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.TableOrder;

/**
 *
 * @author JONATHAN
 */
public class Resultados_Lote_Economico extends javax.swing.JFrame {

    public DefaultTableModel miModelo;
    public String[][] data = {};
    String tipo_cuadro = "", periodo = "", valorY = "", nombre_linea = "Grafico de puntos ingresados";
// Creamos y rellenamos el modelo de datos
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    Resultados_Lote_Economico(Vector ab) {
       initComponents();
        JFreeChart chart = null;
        chart = createChart(dataset, TiposDeGrafico.LineChart);
        // Hacemos y mostramos una ventana con el último de ellos
        ChartPanel grafico = new ChartPanel(chart);
        jPanel1.setLayout(new FlowLayout());
        jPanel1.add(grafico);
        jPanel1.updateUI();

        miModelo = new DefaultTableModel(data, new String[]{"Número de Pedidos", "Tamaño de Compra (Q)", "Costo de Gestion (G)", "Costo de Mantenimiento (CM)", "Costo Total (CT)"}) {
        };
        Tabla_1.setModel(miModelo);
        jScrollPane1.setViewportView(Tabla_1);
        Llenar_Tabla(ab);
    }

    JTable getMiTabla() {
        return Tabla_1;
    }

    public DefaultTableModel getMiModelo() {
        return miModelo;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla_1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtResultado = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtPedidos = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Resultados");

        Tabla_1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Número de Pedidos", "Tamaño de Compra (Q)", "Costo de Gestion (G)", "Costo de Mantenimiento (CM)", "Costo Total (CT)"
            }
        ));
        jScrollPane1.setViewportView(Tabla_1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 654, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 417, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(jPanel1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Lote optimo de compra es:");

        txtResultado.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txtResultado.setText("0");
        txtResultado.setEnabled(false);
        txtResultado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtResultadoActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("El Número de Pedidos sera:");

        txtPedidos.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txtPedidos.setText("0");
        txtPedidos.setEnabled(false);
        txtPedidos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPedidosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtPedidos, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtResultado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtPedidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtResultadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtResultadoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtResultadoActionPerformed

private void txtPedidosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPedidosActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtPedidosActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tabla_1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txtPedidos;
    private javax.swing.JTextField txtResultado;
    // End of variables declaration//GEN-END:variables

    private void Llenar_Tabla(Vector ab) {
        inicializaTabla();
        for(int i=1;i<5;i++){
            double Q,CM,CG,CT;
            Q=Double.parseDouble(ab.get(0).toString()) /i;
            CM=Double.parseDouble(ab.get(0).toString())*Double.parseDouble(ab.get(3).toString())/Q;
            CG=Q*Double.parseDouble(ab.get(1).toString())*Double.parseDouble(ab.get(2).toString())/2;
            CT=CM+CG;
            Object[] datos = {i,Q, CM, CG,CT};
            getMiModelo().addRow(datos);
            dataset.setValue(CM, "CM", String.valueOf(Q));
            dataset.setValue(CG, "CG", String.valueOf(Q));
            dataset.setValue(CT, "CT", String.valueOf(Q));
        }
        txtResultado.setText(Math.sqrt((2*Double.parseDouble(ab.get(0).toString())*Double.parseDouble(ab.get(3).toString()))/(Double.parseDouble(ab.get(1).toString())*Double.parseDouble(ab.get(2).toString())))+"");      
        txtPedidos.setText(Lote_Economico.redondear(((Double.parseDouble(ab.get(0).toString()))/(Math.sqrt((2*Double.parseDouble(ab.get(0).toString())*Double.parseDouble(ab.get(3).toString()))/(Double.parseDouble(ab.get(1).toString())*Double.parseDouble(ab.get(2).toString()))))), 0)+"");
    }

    private void inicializaTabla() {
        // obtiene numero de filas de la tabla
        int filas = getMiTabla().getRowCount();
        // remueve todas las filas de la tabla
        for (int fila = 0; fila < filas; fila++) {
            getMiModelo().removeRow(0);
        }
    }

    private JFreeChart createChart(CategoryDataset dataset, TiposDeGrafico tipo) {
        // Hacemos la ventana con el gráfico
        // Los tres booleans del final:
        // - Si se deben mostrar las leyendas con el nombre
        // - Si se deben mostrar tooltip al poner el ratón sobre las barras
        // - Si se muestran las url
        JFreeChart chart;
        switch (tipo) {
            case AreaChart:
                chart = ChartFactory.createAreaChart(tipo_cuadro, periodo,
                        valorY, dataset, PlotOrientation.VERTICAL, true,
                        true, true);
                break;
            case BarChart:
                chart = ChartFactory.createBarChart(tipo_cuadro, periodo,
                        valorY, dataset, PlotOrientation.VERTICAL, true,
                        true, true);
                break;
            case BarChart3D:
                chart = ChartFactory.createBarChart3D(tipo_cuadro, periodo,
                        valorY, dataset, PlotOrientation.VERTICAL, true,
                        true, true);
                break;
            case LineChart:
                chart = ChartFactory.createLineChart(tipo_cuadro, periodo,
                        valorY, dataset, PlotOrientation.VERTICAL, true,
                        true, true);
                break;
            case LineChart3D:
                chart = ChartFactory.createLineChart3D(tipo_cuadro, periodo,
                        valorY, dataset, PlotOrientation.VERTICAL, true,
                        true, true);
                break;
            case MultiplePieChart:
                chart = ChartFactory.createMultiplePieChart(tipo_cuadro, dataset,
                        TableOrder.BY_COLUMN, true, true, true);
                break;
            case MultiplePieChart3D:
                chart = ChartFactory.createMultiplePieChart3D(tipo_cuadro, dataset,
                        TableOrder.BY_COLUMN, true, true, true);
                break;
            default:
                chart = null;
        }
        return chart;
    }



    enum TiposDeGrafico {

        AreaChart, BarChart, BarChart3D, LineChart, LineChart3D, MultiplePieChart, MultiplePieChart3D
    }
}
